﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using blackhouse.WebNemo.Data;
using System.Data;
using blackhouse.WebNemo.Business.DataTypes;

namespace blackhouse.WebNemo.Business.Groups {
    public class GroupColumn {

        #region Fields

        private GroupColumnData data;
        private DataColumn column;
        private IDataAccess dataAccess;
        private CustomValidatorInfo[] customValidators;

        #endregion

        #region Constructors

        public GroupColumn(GroupColumnData gcd, DataColumn dc, IDataAccess dataAccess) {
            this.dataAccess = dataAccess;
            this.data = gcd;
            this.column = dc;
            if (this.column == null) this.column = new DataColumn(gcd.ColumnName);

            FieldType columnType = FieldType.ValueField;
            if (!Enum.TryParse<FieldType>(gcd.ColumnType, out columnType))
                columnType = FieldType.ValueField;
            this.ColumnType = columnType;
            if (!String.IsNullOrEmpty(gcd.DataTypeController)) {
                Type t = Type.GetType(gcd.DataTypeController, false);
                if (t != null) {
                    this.DataTypeController = Activator.CreateInstance(t) as INemoDataType;
                }
            }
            if (this.DataTypeController == null)
                this.DataTypeController = NULLDataType.Get;

        }

        #endregion

        #region Properties

        public bool AllowNull {
            get {
                return this.column.AllowDBNull;
            }
        }

        public bool AllowUpdateValue {
            get {
                return this.data.Editable;
            }
        }

        public bool EnableTranslate {
            get {
                return this.data.EnableTranslate;
            }
        }

        public string Name { get { return this.data.Name; } }

        public string ColumnName { get { return this.data.ColumnName; } }

        public string SelectColumnName {
            get {
                switch (this.ColumnType) {
                    case FieldType.ValueField:
                    case FieldType.OneReference: return this.data.ColumnName;
                    case FieldType.FileField: return this.data.ColumnName + "_name";
                    case FieldType.ManyReference: return "";
                    default: return this.data.ColumnName;
                }
            }
        }

        public int ColumnId { get { return this.data.ColumnId; } }

        public int BelongsToGroupId { get { return this.data.GroupId; } }

        public FieldType ColumnType { get; private set; }

        public int RelatedGroupId { get { return this.data.RelatedGroupId; } }

        public Type DataType { get { return this.column.DataType; } }

        public object DefaultValue { get { return this.column.DefaultValue; } }

        public int Length { get { return this.column.MaxLength; } }

        public string FormattingValue { get { return this.data.FormattingValue; } }

        public int ColumnSort { get { return this.data.ColumnSort; } }

        public string RegexValidation { get { return this.data.RegexValidation; } }

        public string Description { get { return this.data.Description; } }

        public bool HideForEdition { get { return this.data.HideForEdition; } }

        public string CustomEditControl { get { return this.data.CustomEditControl; } }

        public CustomValidatorInfo[] CustomValidators {
            get {
                if (customValidators == null && this.dataAccess != null)
                    this.customValidators = this.ReadValidators();

                return customValidators;
            }
        }

        public INemoDataType DataTypeController { get; private set; }

        #endregion

        #region Methods

        public string GetCustomEditControlConfiguration() {
            return this.dataAccess.GetCustomEditControlConfiguration(this.ColumnId);
        }

        private CustomValidatorInfo[] ReadValidators() {
            CustomValidatorData[] d = this.dataAccess.GetColumnValueValidators(this.ColumnId);
            CustomValidatorInfo[] dout = new CustomValidatorInfo[d.Length];
            for (int xd = 0; xd < d.Length; xd++) {
                CustomValidatorData di = d[xd];
                CustomValidatorInfo douti = new CustomValidatorInfo();
                douti.ValidatorId = di.ValidatorId;
                douti.ColumnId = di.ColumnId;
                douti.XmlConfiguration = di.XmlConfiguration;
                douti.InstanceClass = di.InstanceClass;
                dout[xd] = douti;
            }
            return dout;
        }

        #endregion

    }
}
